package com.mini.mysql.service;

import com.mini.mysql.model.BusinessStudentBigLessonXcx;
import com.mini.mysql.model.MiniStatisticDataNew;
import com.mini.mysql.repository.MiniStatisticDataNewRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 项目名称：mini-data-es
 * 类 名 称：MiniStatisticDataNewService
 * 类 描 述：TODO
 * 创建时间：2021/3/31 下午5:19
 * 创 建 人：zhanghuiying
 */
@Service
@Slf4j
public class MiniStatisticDataNewService {

    @Autowired
    private MiniStatisticDataNewRepository miniStatisticDataNewRepository;

    public List<MiniStatisticDataNew> findByDate(Specification specification, Pageable pageable) {
        Page all = miniStatisticDataNewRepository.findAll(specification, pageable);
        return all.getContent();
    }

    public Integer countOrder(Date startTime, Date endTime) {
        return miniStatisticDataNewRepository.countByUpdateTimeBetween(startTime, endTime);
    }

    public Page<MiniStatisticDataNew> page(Date startTime, Date endTime, Pageable pageable) {
        Specification<MiniStatisticDataNew> specification = (root, query, cb) -> {
            List<Predicate> list = new ArrayList<>();
            list.add(cb.between(root.get("updateTime"), startTime, endTime));
            return cb.and(list.toArray(new Predicate[list.size()]));
        };
        return miniStatisticDataNewRepository.findAll(specification, pageable);
    }
}
